package com.example.logindemo2.viewmodel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.logindemo2.R
import com.example.logindemo2.model.Constant.ERR_ACCOUNT
import com.example.logindemo2.model.Constant.ERR_NETWORK
import com.example.logindemo2.model.Constant.SP_ACCOUNT
import com.example.logindemo2.model.Constant.SP_PWD
import com.example.logindemo2.model.Constant.SP_RMB_PWD
import com.example.logindemo2.model.Constant.SP_USERNAME
import com.example.logindemo2.model.Constant.STR_NOTHING
import com.example.logindemo2.model.Constant.SUC_RESULT
import com.example.logindemo2.model.requestRequest
import com.example.logindemo2.utils.LoginDemoApplication.Companion.context
import com.example.logindemo2.utils.showFailDialog
import com.example.logindemo2.utils.showToast
import com.example.logindemo2.view.MainActivity.Companion.startMainActivity

class LoginViewModel : ViewModel() {

    var username = MutableLiveData<String>()
    var password = MutableLiveData<String>()
    var rememberPwd = MutableLiveData<Boolean>()

    val sp =
        context.getSharedPreferences(SP_ACCOUNT, Context.MODE_PRIVATE)

    init {
        username.value = STR_NOTHING
        password.value = STR_NOTHING
        rememberPwd.value = sp.getBoolean(SP_RMB_PWD, false)
    }

    //登录
    fun login(username: String, password: String) {
        //记住用户名和密码
        rememberPwd()

        //发起网络请求, 处理响应结果
        requestRequest(username, password) {
            when (it) {
                SUC_RESULT -> {
                    context.getString(R.string.success_login).showToast()
                    startMainActivity(context, true)
                }
                ERR_ACCOUNT -> {
                    context.getString(R.string.error_account).showFailDialog()
                }
                ERR_NETWORK -> {
                    context.getString(R.string.error_network).showFailDialog()
                }
            }
        }
    }

    //记住密码
    fun rememberPwd() {
        val editor = sp.edit()
        if (rememberPwd.value!!) {
            editor.putBoolean(SP_RMB_PWD, true)
            editor.putString(SP_USERNAME, username.value)
            editor.putString(SP_PWD, password.value)
        } else {
            editor.clear()
        }
        editor.apply()
    }

    //恢复密码
    fun recoverPwd() {
        rememberPwd.value = sp.getBoolean(SP_RMB_PWD, false)
        if (rememberPwd.value!!) {
            username.value = sp.getString(SP_USERNAME, STR_NOTHING)
            password.value = sp.getString(SP_PWD, STR_NOTHING)
        } else {
            username.value = STR_NOTHING
            password.value = STR_NOTHING
        }
    }

    fun updateRememberPwdStatus() {
        val editor = sp.edit()
        if (!rememberPwd.value!!) {
            username.value = STR_NOTHING
            password.value = STR_NOTHING
            editor.clear()
            editor.apply()
        }
    }

    //检查用户名和密码是否均符合要求, 用来设置登录按钮是否应该处于激活状态
    fun isLoginButtonActivated(): Boolean {
        return isUsernameValid() && isPasswordValid()
    }

    //用户名不允许为空
    fun isUsernameValid(): Boolean {
        return !username.value!!.isEmpty()
    }

    //检查密码长度至少要求6位
    fun isPasswordValid(): Boolean {
        return password.value!!.length > 5
    }

    //供activity_login.xml中调用测试
    fun test() {
        context.getString(R.string.test_clicked).showToast()
    }
}